package main.java.chessfootball.model.rule;

import main.java.chessfootball.model.game.Half;
import main.java.chessfootball.model.game.Side;
import main.java.chessfootball.model.game.Uniform;
import main.java.chessfootball.model.regulation.Period;

import java.util.List;

/**
 * User: Igor
 * Date: 11.06.2011
 * Time: 9:23:39
 */
public class MatchRule
{
	private List<TimeRule> times;
	private int playersCount;
	private Integer scoreLimit;
	private Integer timeOnTurn;
	private Side startSide;
	private Half startHalf;
	private Uniform[] uniforms;

	public MatchRule(List<TimeRule> times, int playersCount, Integer scoreLimit, Integer timeOnTurn, Side startSide,
					 Half startHalf, Uniform[] uniforms)
	{
		this.times = times;
		this.playersCount = playersCount;
		this.scoreLimit = scoreLimit;
		this.timeOnTurn = timeOnTurn;
		this.startSide = startSide;
		this.startHalf = startHalf;
		this.uniforms = uniforms;
	}

	public TimeRule getTimeRule(Period period)
	{
		for (TimeRule tr : times)
		{
			if (tr.getPeriod().equals(period))
			{
				return tr;
			}
		}
		return null;
	}

	public int getStartTurns(Period period)
	{
		int startTurns = 0;
		for (TimeRule tr : times)
		{
			if (tr.getPeriod().equals(period))
			{
				return startTurns;
			}
			startTurns = startTurns + tr.getTurns();
		}
		throw new IllegalStateException("period " + period + " not found move rules");
	}

	public int getEndTurns(Period period)
	{
		return getStartTurns(period) + getTimeRule(period).getTurns();
	}

	public Side getStartSide(Period period)
	{
		Side side = startSide;
		for (TimeRule tr : times)
		{
			if (tr.getPeriod().equals(period))
			{
				return side;
			}
			side = side.getInvert();
		}
		throw new IllegalStateException("period " + period + " not found move rules");
	}

	public Half getStartHalf(Period period)
	{
		Half half = startHalf;
		for (TimeRule tr : times)
		{
			if (tr.getPeriod().equals(period))
			{
				return half;
			}
			half = half.getInvert();
		}
		throw new IllegalStateException("period " + period + " not found move rules");
	}

	public List<TimeRule> getTimes()
	{
		return times;
	}

	public int getPlayersCount()
	{
		return playersCount;
	}

	public Integer getScoreLimit()
	{
		return scoreLimit;
	}

	public Integer getTimeOnTurn()
	{
		return timeOnTurn;
	}

	public Side getStartSide()
	{
		return startSide;
	}

	public Half getStartHalf()
	{
		return startHalf;
	}

	public Uniform[] getUniforms()
	{
		return uniforms;
	}
}
